package com.poly.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.poly.common.core.tools.utils.Func;
import com.poly.common.core.utils.AssertUtil;
import com.poly.common.core.utils.ConvertUtils;
import com.poly.system.domain.SysAppMenu;
import com.poly.system.mapper.SysAppMenuMapper;
import com.poly.system.pojo.dto.SysAppMenuDTO;
import com.poly.system.pojo.query.SysAppMenuQuery;
import com.poly.system.pojo.vo.SysAppMenuVO;
import com.poly.system.service.ISysAppMenuAuthService;
import com.poly.system.service.ISysAppMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 应用菜单Service业务层处理
 *
 * @author poly
 * @date 2024-01-02
 */
@Service
public class SysAppMenuServiceImpl extends ServiceImpl<SysAppMenuMapper, SysAppMenu> implements ISysAppMenuService {
    @Autowired
    private ISysAppMenuAuthService appMenuAuthService;

    /**
     * 获取查询对象
     *
     * @param query
     * @return
     */
    public LambdaQueryWrapper<SysAppMenu> getWrapper(SysAppMenuQuery query) {
        LambdaQueryWrapper<SysAppMenu> wrapper = null;
        if (Func.isNotEmpty(query)) {
            wrapper = Wrappers.<SysAppMenu>lambdaQuery();
            if (Func.isNotEmpty(query.getId())) {
                wrapper.eq(SysAppMenu::getId, query.getId());
            }
            if (Func.isNotEmpty(query.getComponent())) {
                wrapper.eq(SysAppMenu::getComponent, query.getComponent());
            }
            if (Func.isNotEmpty(query.getIcon())) {
                wrapper.eq(SysAppMenu::getIcon, query.getIcon());
            }
            if (Func.isNotEmpty(query.getName())) {
                wrapper.like(SysAppMenu::getName, query.getName());
            }
            if (Func.isNotEmpty(query.getParentId())) {
                wrapper.eq(SysAppMenu::getParentId, query.getParentId());
            }
            if (Func.isNotEmpty(query.getSort())) {
                wrapper.eq(SysAppMenu::getSort, query.getSort());
            }
            if (Func.isNotEmpty(query.getAuthType())) {
                wrapper.eq(SysAppMenu::getAuthType, query.getAuthType());
            }
            if (Func.isNotEmpty(query.getPackageName())) {
                wrapper.like(SysAppMenu::getPackageName, query.getPackageName());
            }
            if (Func.isNotEmpty(query.getStatus())) {
                wrapper.eq(SysAppMenu::getStatus, query.getStatus());
            }
        }
        return wrapper;
    }

    @Override
    public Long add(SysAppMenuDTO dto) {
        SysAppMenu entity = ConvertUtils.convert(dto, SysAppMenu.class);
        save(entity);
        return entity.getId();
    }

    @Override
    public boolean update(SysAppMenuDTO dto) {
        SysAppMenu entity = ConvertUtils.convert(dto, SysAppMenu.class);
        return updateById(entity);
    }

    public void delete(List<Long> menuIdList) {
        AssertUtil.isTrue(Func.isNotEmpty(menuIdList), "参数错误");
        removeBatchByIds(menuIdList);
    }

    @Override
    public List<SysAppMenuVO> list(SysAppMenuQuery query) {
        return ConvertUtils.convert(list(getWrapper(query)), SysAppMenuVO.class);
    }

    /**
     * @param orgId
     * @param roleIdList
     * @param userId
     * @return
     */
    public List<SysAppMenuVO> menuTree(Long orgId, List<Long> roleIdList, Long userId) {
        //登录用户相关授权菜单
        //List<Long> menuIdList = appMenuAuthService.getMenuIdListByOrgId(orgId, roleIdList, userId);
        List<Long> menuIdList= null;
        List<SysAppMenuVO> menuTreeList = null;
        List<SysAppMenu> list = list(Wrappers.<SysAppMenu>lambdaQuery().in(Func.isNotEmpty(menuIdList),SysAppMenu::getId, menuIdList));
        menuTreeList = getChildPerms(list, 0L);
        return menuTreeList;
    }

    /**
     * 根据父节点的ID获取所有子节点
     */
    public List<SysAppMenuVO> getChildPerms(List<SysAppMenu> list, Long parentId) {
        List<SysAppMenuVO> functionItemTrees = new ArrayList<>();
        if (Func.isNotEmpty(list)) {
            for (SysAppMenu functionItem : list) {
                if (functionItem.getParentId().equals(parentId)) {
                    SysAppMenuVO dto = ConvertUtils.convert(functionItem, SysAppMenuVO.class);
                    dto.setChildren(recursion(list, functionItem));
                    functionItemTrees.add(dto);
                }
            }
        }
        return functionItemTrees;
    }

    private List<SysAppMenuVO> recursion(List<SysAppMenu> list, SysAppMenu parent) {
        List<SysAppMenuVO> result = new ArrayList<>();
        for (SysAppMenu functionItem : list) {
            if (functionItem.getParentId().equals(parent.getId())) {
                List<SysAppMenuVO> children = recursion(list, functionItem);
                SysAppMenuVO dto = ConvertUtils.convert(functionItem, SysAppMenuVO.class);
                dto.setChildren(children);
                result.add(dto);
            }
        }
        return result;
    }
}
